/*
 * ArCoLIVE - Live Internet Videoconference Environment
 * Information Technology Departament - Federal University of Alagoas - Brazil
 *
 * Copyright (C) 2005 Leandro Melo de Sales (leandro@labpesquisas.tci.ufal.br)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package br.ufal.tci.nexos.arcolive.service.broker;

import br.ufal.tci.nexos.arcolive.command.ArCoLIVECommandResult;
import br.ufal.tci.nexos.arcolive.exception.ArCoLIVECannotProcessCommandException;
import br.ufal.tci.nexos.arcolive.protocol.ArCoLIVEProtocol;
import br.ufal.tci.nexos.arcolive.service.broker.command.GetAvailableServicesCommand;
import br.ufal.tci.nexos.arcolive.service.broker.command.GetParticipantListOfServiceInstanceCommand;
import br.ufal.tci.nexos.arcolive.service.broker.command.GetServiceInstancesCommand;
import br.ufal.tci.nexos.arcolive.service.broker.command.LoginServiceCommand;
import br.ufal.tci.nexos.arcolive.service.broker.command.LogoutServiceCommand;
import br.ufal.tci.nexos.arcolive.service.broker.command.SetParticipantProfileCommand;
import br.ufal.tci.nexos.arcolive.service.broker.command.StatusServiceCommand;
import br.ufal.tci.nexos.arcolive.service.connection.ConnectionRequest;

/**
 * CLASSNAME.java
 *
 * CLASS DESCRIPTION
 *
 * @see CLASSNAME
 *
 * @author <a href="mailto:felipe@labpesquisas.tci.ufal.br">Felipe Barros Pontes</a>.
 * @author <a href="mailto:leandro@labpesquisas.tci.ufal.br">Leandro Melo de Sales</a>.
 * @since 0.1
 * @version 0.1
 *
 * <p><b>Revisions:</b>
 *
 * <p><b>yyyymmdd USERNAME:</b>
 * <ul>
 * <li> VERSION
 * </ul>
 */
public class ArCoLIVEServiceBrokerProtocol extends ArCoLIVEProtocol {

    //Protocol Commands
	public static final int ID                           	= 200;
	public static final int GET_SERVICES                    = 201;
	public static final int GET_SERVICEINSTANCES            = 202;
	public static final int LOGIN_SERVICEINSTANCE           = 203;
	public static final int LOGOUT_SERVICEINSTANCE          = 204;
	public static final int STATUS_SERVICEINSTANCE         	= 205;
	public static final int PARTICIPANTLIST_SERVICEINSTANCE	= 206;
	public static final int SETPARTICIPANTPROFILE           = 207;
	
	//Protocol Commands' Description
    private static String[] identifiersDescriptions = {
    	"Available services request",
    	"Instances of service request",
    	"Login in service request",
    	"Logout from service request",
    	"Service instance status request",
    	"Participant instance List request",
    	"Change participant name request",
    	"Change participant email request"
    };
    
    public ArCoLIVEServiceBrokerProtocol() {
    	super(ID);
    }
	
    protected String encodeMessageImpl(int commandCode) {
		return commandCode + "#" + identifiersDescriptions[commandCode-ID-1];
	}

	protected ArCoLIVECommandResult handleRequestImpl(ConnectionRequest request) throws ArCoLIVECannotProcessCommandException {
		ArCoLIVECommandResult commandResult = null;
		
		int commandId;
		try {
			commandId = request.getIntegerParameter("commandId").intValue();
		} catch (Exception e) {
			commandId = -1;
		}		
		
		switch (commandId) {
			case GET_SERVICES:
				commandResult = (new GetAvailableServicesCommand(request)).execute();
				break;
				
			case GET_SERVICEINSTANCES:
				commandResult = (new GetServiceInstancesCommand(request)).execute();
				break;
				
			case LOGIN_SERVICEINSTANCE:
				commandResult = (new LoginServiceCommand(request)).execute();
				break;
				
			case LOGOUT_SERVICEINSTANCE:
				commandResult = (new LogoutServiceCommand(request)).execute();
				break;
				
			case STATUS_SERVICEINSTANCE:
				commandResult = (new StatusServiceCommand(request)).execute();
				break;
				
			case PARTICIPANTLIST_SERVICEINSTANCE:
				commandResult = (new GetParticipantListOfServiceInstanceCommand(request)).execute();
				break;
				
			case SETPARTICIPANTPROFILE:
				commandResult = (new SetParticipantProfileCommand(request)).execute();
				break;
				
			default:
				throw new ArCoLIVECannotProcessCommandException("No such command for the Service Broker Protocol");
		}

		return commandResult;
	}	
	
}
